﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Activities.Presentation.Model;
using System.Activities;
using Microsoft.VisualBasic.Activities;
using System.Windows.Controls;
using System.Activities.Expressions;

namespace TransactionServer.ActivityDesign.Helpers
{
    class ComboBoxItemBoolConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            ModelItem modelItem = value as ModelItem;
            if (value != null)
            {
                if (modelItem.GetCurrentValue() is InArgument<Boolean>)
                {
                    InArgument<Boolean> inArgument = modelItem.GetCurrentValue() as InArgument<Boolean>;

                    if (inArgument != null)
                    {
                        Activity<Boolean> expression = inArgument.Expression;
                        Literal<Boolean> literal = expression as Literal<Boolean>;

                        if (literal != null)
                        {
                            if (literal.Value == true)
                                return "Да";
                            if (literal.Value == false)
                                return "Нет";
                        }
                    }
                }
                
            }
            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            String itemContent = (String)value;

            if (targetType == typeof(InArgument<Boolean>))
            {
                VisualBasicValue<Boolean> vbArgument = null;
                if (itemContent == "Да")
                    vbArgument = new VisualBasicValue<Boolean>("True");
                if (itemContent == "Нет")
                    vbArgument = new VisualBasicValue<Boolean>("False");
                return new InArgument<Boolean>(vbArgument);
            }
            return null;
        }
    }
}
